<?php
App::uses('AppModel', 'Model');
/**
 * Ticket Model
 *
 * @property SysWebsite $SysWebsite
 * @property SysSeo $SysSeo
 * @property TicketCate $TicketCate
 * @property Venue $Venue
 * @property ViewPhoto $ViewPhoto
 * @property TicketBook $TicketBook
 * @property TicketPrice $TicketPrice
 */
class Ticket extends AppModel {
	
	public $actsAs = array('Pagination');

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'title';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'sys_website_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ticket_cate_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'venue_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'title' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'date_start' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'date_end' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'showtime' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'sort' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'SysWebsite' => array(
			'className' => 'SysWebsite',
			'foreignKey' => 'sys_website_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'SysSeo' => array(
			'className' => 'SysSeo',
			'foreignKey' => 'sys_seo_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'TicketCate' => array(
			'className' => 'TicketCate',
			'foreignKey' => 'ticket_cate_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'TicketScate' => array(
				'className' => 'TicketCate',
				'foreignKey' => 'ticket_scate_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		),
		'Venue' => array(
			'className' => 'Venue',
			'foreignKey' => 'venue_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'ViewPhoto' => array(
			'className' => 'SysPhoto',
			'foreignKey' => 'view_photo_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'ListPhoto' => array(
				'className' => 'SysPhoto',
				'foreignKey' => 'list_photo_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'TicketPrice' => array(
			'className' => 'TicketPrice',
			'foreignKey' => 'ticket_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'TicketAdvisory' => array(
				'className' => 'TicketAdvisory',
				'foreignKey' => 'ticket_id',
				'dependent' => false,
				'conditions' => array('TicketAdvisory.ticket_show'=>1),
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'exclusive' => '',
				'finderQuery' => '',
				'counterQuery' => ''
		)
	);

	/**
	 * 分页条件
	 * @author mingxing.zhao@qq.com
	 * @Date 2012-12-20
	 * @return Array
	 */
	function pageParam($action){
		return array(
				"colNames"=>array(
						'ID','站点','票品名称','分类','场馆','上映','开始','结束','点击','创建时间','操作'
				),
				"colModel"=>array(
						array("name"=>'Ticket.id',"width"=>'50',"sortable"=>true,"align"=>"right"),
						array("name"=>'Ticket.sys_website_id',"width"=>"80","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.title',"width"=>"200","sortable"=>true,"align"=>"left"),
						array("name"=>'Ticket.ticket_cate_id',"width"=>"100","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.venue_id',"width"=>"100","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.showtime',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.date_start',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.date_end',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.count',"width"=>"30","sortable"=>true,"align"=>"center"),
						array("name"=>'Ticket.created',"width"=>"110","sortable"=>true,"align"=>"center"),
						array("name"=>'act',"width"=>"150","sortable"=>false,"align"=>"center")
				),
				"action"=>$action,
				'sortname'=>'id',
				'sortorder'=>'DESC'
		);
	}
	
	/**
	 * 分页数据
	 * @author mingxing.zhao@qq.com
	 * @param array
	 * @date 2013-12-20
	 * @return json
	 *
	 */
	function jsonData($res){
		$rowN = array();
		$i=0;
		foreach($res['rows'] as $k => $value){
			$rowN[$i]['id'] = $value['Ticket']['id'];
			$rowN[$i++]['cell'] = array(
					$value['Ticket']['id'],
					$value['SysWebsite']['title'],
					$value['Ticket']['title'],
					$value['TicketCate']['title_zh'],
					$value['Venue']['title_cn'],
					date('m-d',strtotime($value['Ticket']['showtime'])),
					date('m-d',strtotime($value['Ticket']['date_start'])),
					date('m-d',strtotime($value['Ticket']['date_end'])),
					$value['Ticket']['count'],
					$value['Ticket']['created'],
					''
			);
		}
		$res['rows'] = $rowN;
		return json_encode($res);
	}
	
	/**
	 * 获取分类下票品
	 * @author mingxing.zhao@qq.com
	 * @date 2014-1-8 下午7:48:59
	 * @param variable
	 * @return 
	 */
	function getCateTicket($cate_id = false){
		return $this->find('all',array(
				'fields'=>array(
					'Ticket.title','Ticket.*'
				),
				'conditions'=>array(
					'OR'=>array(
						'Ticket.ticket_cate_id'=>$cate_id,
						'Ticket.ticket_scate_id'=>$cate_id,
					),
				    'Ticket.enable'=>1
				),
				'limit'=>7,
				'order'=>'Ticket.sort_top DESC,Ticket.sort ASC'
		));
	}
	
	function updateMaxMinPrince($ticket_id){
		$price = $this->TicketPrice->find('first',array(
			'fields'=>array('max(TicketPrice.price) as MaxP','min(TicketPrice.price) as MinP'),
			'conditions'=>array(
				'TicketPrice.ticket_id'=>$ticket_id
			)
		));
		$ca = array(
				'Ticket'=>array(
						'id'=>$ticket_id,
						'price_max'=>$price[0]['MaxP'],
						'price_min'=>$price[0]['MinP']
				)
		);
		$this->save($ca);
	}
	
	function relateTicket($tags,$limit = 5,$not = false){
		if(strlen($tags)==0){
			return array();
		}else{
			$tagArr = split(',', $tags);
			$conditions = array("OR"=>array());
			foreach ($tagArr as $tag){
				$tag = trim($tag);
				$conditions['OR'][] = array('Ticket.tags like'=>'%'.$tag.'%');
			}
			if($not){
				$conditions['Ticket.id <>'] = $not;
			}
			$conditions['Ticket.enable'] = 1;
			$this->recursive = -1;
			return $this->find('all',array(
					'fields'=>array('Ticket.id','Ticket.title','Ticket.ticket_cate_id','Ticket.ticket_scate_id','Ticket.showtime'),
					'conditions'=>$conditions,
					'limit'=>$limit,
					'order'=>'Ticket.id DESC',
					'group'=>'Ticket.id'
			));
		}
	}
	
	function recommendTicket($limit = 6){
		return $this->find('all',array(
				'fields'=>array('Ticket.id','Ticket.title','Ticket.ticket_cate_id','Ticket.ticket_scate_id','Ticket.showtime'),
				'conditions'=>array('Ticket.sort_top'=>1,'Ticket.enable'=>1),
				'limit'=>$limit,
				'order'=>'Ticket.id DESC',
				'group'=>'Ticket.id'
		));
	}
	
	function newTicket($limit = 6){
		return $this->find('all',array(
				'fields'=>array('Ticket.id','Ticket.title','Ticket.ticket_cate_id','Ticket.ticket_scate_id','Ticket.showtime'),
		        'conditions'=>array('Ticket.enable'=>1),
				'limit'=>$limit,
				'order'=>'Ticket.created DESC'
		));
	}
}
